package com.mzj.saas.jobs.backlog.repository;

import com.mzj.saas.jobs.backlog.entity.SmartLock;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;

import java.util.List;

public interface SmartLockRepository extends PagingAndSortingRepository<SmartLock,Long>, JpaSpecificationExecutor<SmartLock> {
	
	List<SmartLock> findByHouseIdIn(List<String> houseId);

	List<SmartLock> findBySnIn(List<String> sns);

	List<SmartLock> findByIdIn(List<Long> ids);

	SmartLock findBySn(String sn);

	SmartLock findByMatchUserId(String matchUserId);

	List<SmartLock> findByHouseId(String houseId);

	List<SmartLock> findByHouseIdOrMac(String houseId, String mac);

	SmartLock findByMac(String mac);

	@Query("from SmartLock where sn = ?1 and mechineStatus=?2")
	SmartLock findBySnAndMechineStatus(String sn, Integer mechineStatus);

	@Modifying
	@Query(value = "UPDATE saas_smart_lock SET mechine_status = ?2 WHERE id = ?1", nativeQuery = true)
	int updateMechineStatusById(Long id, Integer mechineStatus);

	@Query(value = "SELECT l.id AS lockId FROM `saas_smart_lock` l LEFT JOIN `saas_house` h ON l.`house_id` = h.id WHERE h.id = ?1", nativeQuery = true)
	Long findByHouseIds(String houseId);

	List<SmartLock> findByOperatorId(String operatorId);
	
}
